package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 地址新增
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        //获取到线程中存储的id,设置到addressbook中
        System.out.println(BaseContext.getCurrendId());
        addressBook.setUserId(BaseContext.getCurrendId());
        log.info("addressBook:{}", addressBook);

        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        //条件构造器
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId, BaseContext.getCurrendId());
        qw.set(AddressBook::getIsDefault, 0);
        //调用业务层中的update方法,将所有默认字段改成0
        addressBookService.update(qw);
        //将前端获取到的数据里的默认字段改为1
        addressBook.setIsDefault(1);
        //调用业务层方法,根据传进来的id修改数据库
        addressBookService.updateById(addressBook);
        //返回结果
        return R.success(addressBook);
    }

    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        //根据id查询对应的数据
        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //用用户id为条件
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrendId());
        //在根据is_default为条件
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //用getOne获取查询到的对象
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        //判断是否返回来值
        if (addressBook == null) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        //浏览器传输过来的数据里userid是没有的
        //所以得从线程中获取到userId设置到addressBook中
        addressBook.setUserId(BaseContext.getCurrendId());
        log.info("addressBook:{}", addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id为查询的条件
        queryWrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,addressBook.getUserId());
        //根据更改时间排序
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        //调用业务层的自带方法list,返回去
        return R.success(addressBookService.list(queryWrapper));
    }


}
